package com.thrc.borrow.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.Menu;
import com.thrc.borrow.service.MenuService;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.MenuVo;

@Controller
@RequestMapping("/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;

	@RequestMapping("/goMenuJsp")
	public String goMenuJsp() {

		return "sys/menuList";
	}

	@RequestMapping("/load4add")
	public String load4add() {

		return "sys/menuAdd";
	}

	@RequestMapping("/getMenuById")
	public void selectById(HttpServletResponse response, Menu t) {

		ResponseUtil.print(response, menuService.selectById(t));
	}

	@RequestMapping("/selectAllMenu")
	public void selectAll(HttpServletResponse response, Menu t) {

		try {
			List<MenuVo> menuList = menuService.selectAll(t);
			if (Menu.TYPE_LIST.equals(t.getType())) {
				MenuVo nummVo = new MenuVo();
				nummVo.setId(-1);
				nummVo.setName("-空-");
				menuList.add(nummVo);
			}
			ResponseUtil.print(response, menuList);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}

	}

	@RequestMapping("/selectMenuListByPage")
	public void selectByPage(HttpServletResponse response, Menu t) {

		JSONObject retJson = new JSONObject();
		try {
			int totleCount = menuService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<MenuVo> menuList = menuService.selectByPage(t);
			retJson.put("data", menuList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/updateMenu")
	public void update(HttpServletResponse response, Menu t) {

		try {
			menuService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/deleteMenu")
	public void delete(HttpServletResponse response, Menu t) {

		try {
			menuService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/insertMenu")
	public void insert(HttpServletResponse response, Menu t) {

		try {
			menuService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

}
